package com.springcloud.sc.ucpv5.client.module.setting;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import com.springcloud.sc.ucpv5.client.common.ini.IniReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SettingDataManager {

	private static final Logger logger = LoggerFactory.getLogger(SettingDataManager.class);
	private static SettingDataManager instance;
	// 项目物理路径
	public static String relativePath = "relativePath";

	public static SettingDataManager getInstance() {
		if (instance == null) {
			instance = new SettingDataManager();
		}
		return instance;
	}

	private IniReader currentIni;

	// 数据库属性文件
	private Properties configProperties = null;

	public SettingDataManager() {
		currentIni = new IniReader();
	}

	public IniReader getCurrentSetting() {
		return currentIni;
	}

	public String ReadIni(String pcSection, String pcKey) {
		String value = currentIni.getValue(pcSection, pcKey);
		return value;
	}

	public void WriteIni(String pcSection, final String pcKey, final String pcVal) {
		{
			// 不重写文件
			currentIni.setItemValue(pcSection, pcKey, pcVal);
		}
	}

	// 设置定义值
	public void setIniValue(String section, String variable, String value) {
		{
			// 不重写文件
			currentIni.setItemValue(section, variable, value);
		}
	}

	/**
	 * 定义从ini取得的文字
	 * 
	 * @param content
	 * @param field
	 * @param defaultValue
	 * @return
	 */
	public String getStringValue(String content, String field, String defaultValue) {
		String value = getInstance().ReadIni(content, field);
		if (value == null || "".equals(value)) {
			value = defaultValue;
		}
		return value;
	}

	public Integer getIntegerValue(String pcSection, String pcKey, int defaultValue) {
		String value = getInstance().ReadIni(pcSection, pcKey);
		if (value != null && !"".equals(value)) {
			try {
				return Integer.parseInt(value);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		return defaultValue;
	}

	public Boolean getBooleanValue(String pcSection, String pcKey, Boolean defaultValue) {
		String value = getInstance().ReadIni(pcSection, pcKey);
		if (value != null && !"".equals(value)) {
			try {
				Boolean b = Boolean.parseBoolean(value);
				return b;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		return defaultValue;
	}

	public String GetProValue(String Key) {
		return configProperties.getProperty(Key);
	}

}
